package cn.jufuns.util;

import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class ArrayUtil {

	private static final Log log = LogFactory.getLog(ArrayUtil.class);

	/**
	 * 判断集合是否有数据
	 * 
	 * @param collection
	 * @return
	 */
	public static boolean isEmpty(Collection<?> collection) {
		return (collection == null) || (collection.size() == 0);
	}

	public static boolean isNotEmpty(Collection collection){
	        return !isEmpty(collection);
	}
	/**
	 * 判断Object[]是否有数据
	 * 
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(Object[] objects) {
		return (objects == null) || (objects.length == 0);
	}

	public static boolean isNotEmpty(Object array[]){
	        return !isEmpty(array);
	}

	
	/**
	 * 判断Map是否有数据
	 * 
	 * @param array
	 * @return
	 */
	public static boolean isEmpty(Map<?, ?> map) {
		return (map == null) || (map.size() == 0);
	}

	public static boolean isNotEmpty(Map map)
	{
	        return !isEmpty(map);
	}
	/**
	 * object[]转成list存储
	 * 
	 * @param objs
	 * @return
	 */
	public static List<Object> toAsList(Object[] objs) {
		return Arrays.asList(objs);
	}

	/**
	 * 数组分割成字符
	 * 
	 * @param objs
	 *            数组
	 * @param mark
	 *            符合
	 * @return
	 */
	public static String getStrByArray(Object[] objs, String mark) {
		if (objs == null || objs.length == 0)
			return "";
		else {
			StringBuffer returnStr = new StringBuffer("");
			for (int i = 0, j = objs.length; i < j; i++) {
				if (null != objs[i]) {
					if (objs[i] instanceof Date) {
						returnStr.append(
								DateUtil.formateTime2String((Date) objs[i],
										"yyyy-MM-dd HH:mm:ss")).append(mark);
					} else {
						returnStr.append(objs[i]).append(mark);
					}
				}
			}
			returnStr.deleteCharAt(returnStr.length()-mark.length());
			return returnStr.toString();
		}
	}

	/**
	 * 判断数组中是否包括某个对象
	 * 
	 * @param array
	 * @param obj
	 * @return
	 */
	public static boolean contains(Object[] array, Object obj) {
		boolean flag = false;
		if (isEmpty(array)) {
			throw new RuntimeException("array is empty or is null");
		}
		for (int i = 0, len = array.length; i < len; i++) {
			if (array[i].equals(obj)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 去掉集合中存储字符串数组的空格，回车等字符
	 * @param collection
	 * @return
	 */
	public static Collection repeatTrim(Collection<String[]> collection) {
		if (isEmpty(collection))
			return null;
		for (String[] strs : collection) {
			for(int i=0,len=strs.length;i<len;i++){
				strs[i] = PatternUtil.removeBlack(strs[i]);
			}
		}
		return collection;
	}

	public static int indexOf(Object array[], Object obj)
	{
	        if(isEmpty(array))
	            throw new RuntimeException("array is empty or is null");
	        for(int i = 0; i < array.length; i++)
	            if(array[i].equals(obj))
	                return i;

	        return -1;
	 }
	
	/**
	 * 返回传入参数集合都有的key值的新集合
	 * @param a
	 * @param b
	 * @return
	 */
	 public static List compareA1B1(List a, List b)
	    {
	        List result = new ArrayList();
	        if(isEmpty(a) && isEmpty(b))
	            return result;
	        if(isEmpty(a) && isNotEmpty(b))
	            return result;
	        if(isNotEmpty(a) && isEmpty(b))
	            return result;
	        int aLen = a.size();
	        int bLen = b.size();
	        if(aLen >= bLen)
	        {
	            for(Iterator iterator = b.iterator(); iterator.hasNext();)
	            {
	                Object obj = (Object)iterator.next();
	                if(a.contains(obj))
	                    result.add(obj);
	            }

	        } else
	        {
	            for(Iterator iterator1 = a.iterator(); iterator1.hasNext();)
	            {
	                Object obj = (Object)iterator1.next();
	                if(b.contains(obj))
	                    result.add(obj);
	            }

	        }
	        return result;
	    }
	 	/**
	 	 * 返回第1个传入参数集合中第2个参数集合没有的key值的新集合
	 	 * @param a
	 	 * @param b
	 	 * @return
	 	 */
	    public static List compareA1B0(List a, List b)
	    {
	        List result = new ArrayList();
	        if(isEmpty(a) && isEmpty(b))
	            return result;
	        if(isEmpty(a) && isNotEmpty(b))
	            return result;
	        if(isNotEmpty(a) && isEmpty(b))
	            return a;
	        for(Iterator iterator = a.iterator(); iterator.hasNext();)
	        {
	            Object obj = (Object)iterator.next();
	            if(!b.contains(obj))
	                result.add(obj);
	        }

	        return result;
	    }
	    /**
	 	 * 返回第2个传入参数集合中第1个参数集合没有的key值的新集合
	 	 * @param a
	 	 * @param b
	 	 * @return
	 	 */
	    public static List compareA0B1(List a, List b)
	    {
	        List result = new ArrayList();
	        if(isEmpty(a) && isEmpty(b))
	            return result;
	        if(isEmpty(a) && isNotEmpty(b))
	            return result;
	        if(isNotEmpty(a) && isEmpty(b))
	            return a;
	        for(Iterator iterator = b.iterator(); iterator.hasNext();)
	        {
	            Object obj = (Object)iterator.next();
	            if(!a.contains(obj))
	                result.add(obj);
	        }

	        return result;
	    }
	    
	    /**
	     * 返回集合中第一个值
	     * @param collection
	     * @return
	     */
	    public static Object getTopOneElement(Collection collection)
	    {
	        if(isEmpty(collection))
	            throw new RuntimeException("collection is emptys");
	        Object e = null;
	        Iterator it = collection.iterator();
	        if(it.hasNext())
	        {
	            e = it.next();
	            return e;
	        } else
	        {
	            return null;
	        }
	    }
	    
	    
	    
	    
	    
	    
	public static void main(String[] args) {
		List<String[]> list = new ArrayList<String[]>();
		list.add(new String[]{"123  ","\r\n 123 "});
		list.add(new String[]{"456  "," 4 5 6  ","</br>1234 "});
		repeatTrim(list);
		for(int i=0;i<list.size();i++){
			String[] strs = list.get(i);
			for(String str :strs){
				System.out.println(">>>>>"+str);
			}
		}
		System.out.println(getStrByArray(new Object[]{false,"123"}, ","));
		
	}
	
}